import { Effect, Reducer,qiankunStart } from 'umi';
import { query } from '@/services/qiankun';

function sleep(ms:number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

export interface QiankunModelState {
  apps?:{
    name: string,
    entry: string,
    base: string,
    mountElementId: string
  }[] ;
}

export interface UserModelType {
  namespace: 'qiankun';
  state: QiankunModelState;
  effects: {
    getApps: Effect;
  };
  reducers: {
    getAppsSuccess: Reducer<QiankunModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'qiankun',

  state: {
    apps: [],
  },

  effects: {
    *getApps(_, { call,put }) {
      /*
       子应用配置信息获取分同步、异步两种方式
       同步有两种配置方式，1、app.js导出qiankun对象，2、配置写在umi配置文件中，可通过import @tmp/subAppsConfig获取
      */
      console.log('waiting for qiankun start');
      yield sleep(1000);

      const apps = yield call(query);
      console.log(apps);
      yield put({
        type: 'getAppsSuccess',
        payload: {
          apps,
        },
      });

      // 模拟手动控制 qiankun 启动时机的场景, 需要 defer 配置为 true
      setTimeout(qiankunStart, 1000);
    },
  },

  reducers: {
    getAppsSuccess(state, action) {
      return {
        ...state,
        apps: action.payload,
      };
    }
  },
};

export default UserModel;
